﻿package src.utils 
{
	import src.debug.MonsterDebugger;
	import flash.events.*;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import src.data.ConfigData;
	
	
	/**
	 * ...
	 * @author hrh - 2009
	 */
	
	public class ConfigImport extends EventDispatcher
	{
		private var config:ConfigData;
		private var loader:URLLoader;
		private var urlRequest:URLRequest;
		private var xml:XML;
		
		public function ConfigImport(url:String) 
		{
			config =  new ConfigData;
			loader = new URLLoader;
			urlRequest = new URLRequest(url);
			
			loader.load(urlRequest);
			loader.addEventListener(Event.COMPLETE, onComplete);
			loader.addEventListener(IOErrorEvent.IO_ERROR , onFail);
		}
		
		public function get data():ConfigData
		{
			return config;
		}
		
		private function onComplete(e:Event):void
		{
			xml = XML(e.target.data);
			
			for each(var i in xml.children())
			{
				config[i.localName()] = i;
			}
			
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		private function onFail(e:Event):void
		{
			MonsterDebugger.trace(this, 'CONFIG DATA FAILED TO LOAD!');
		}
	}
}